##################################################################################
##Code for "Separated by Politics? Disentangling the Dimensions of Discrimination"
##Deichert, Goggin, Theodoridis
##Last updated December 2021
##Email goggin@berkeley.edu with any questions
##################################################################################

library(cregg)
library(car)
library(ggplot2)
library(FindIt)

data <- read.csv("replication.csv")

#Function for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){ 
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

#########
##Now, need to stack into conjoint trials

#conj_treat is treatments:
#     Count   Code   Label
#          -----   ----   -----
#            534      1   Relative
#            516      2   Neighbor
#            538      3   Loan
#            539      4   Charity
#            527      5   Jury

##DVs are conj_1, conj_2, conj_3, conj_4, conj_5 w/ 1=="Person A", 2=="Person B"
#most_import_factor is choice of factor in choices

##Building a data frame where each row is a target person

attach(data)

stacked <- data.frame(
caseid=rep(caseid,10),
weight=rep(weight,10),
newsint=rep(newsint,10),
##Respondent Characteristics
resp_race=rep(race,10),
resp_sex=rep(gender,10),
resp_age=rep(birthyr,10),
resp_pid7=rep(pid7,10),
resp_ideo=rep(ideo5,10),
resp_grew=rep(inputstate,10),
resp_relig=rep(religpew,10),
resp_evang=rep(pew_bornagain,10),
resp_career=rep(occupation,10),
resp_marstat=rep(marstat,10),
resp_sexorient=rep(sexual_orientation,10),
##Conjoint Type
conj_treat=rep(conj_treat,10),
##Factors
factor_race=c(conj_1A_race,conj_1B_race,conj_2A_race,conj_2B_race,conj_3A_race,conj_3B_race,conj_4A_race,conj_4B_race,conj_5A_race,conj_5B_race),
factor_sex=c(conj_1A_sex,conj_1B_sex,conj_2A_sex,conj_2B_sex,conj_3A_sex,conj_3B_sex,conj_4A_sex,conj_4B_sex,conj_5A_sex,conj_5B_sex),
factor_age=c(conj_1A_age,conj_1B_age,conj_2A_age,conj_2B_age,conj_3A_age,conj_3B_age,conj_4A_age,conj_4B_age,conj_5A_age,conj_5B_age),
factor_party=c(conj_1A_party,conj_1B_party,conj_2A_party,conj_2B_party,conj_3A_party,conj_3B_party,conj_4A_party,conj_4B_party,conj_5A_party,conj_5B_party),
factor_ideo=c(conj_1A_ideo,conj_1B_ideo,conj_2A_ideo,conj_2B_ideo,conj_3A_ideo,conj_3B_ideo,conj_4A_ideo,conj_4B_ideo,conj_5A_ideo,conj_5B_ideo),
factor_grew=c(conj_1A_grew,conj_1B_grew,conj_2A_grew,conj_2B_grew,conj_3A_grew,conj_3B_grew,conj_4A_grew,conj_4B_grew,conj_5A_grew,conj_5B_grew),
factor_relig=c(conj_1A_relig,conj_1B_relig,conj_2A_relig,conj_2B_relig,conj_3A_relig,conj_3B_relig,conj_4A_relig,conj_4B_relig,conj_5A_relig,conj_5B_relig),
factor_career=c(conj_1A_career,conj_1B_career,conj_2A_career,conj_2B_career,conj_3A_career,conj_3B_career,conj_4A_career,conj_4B_career,conj_5A_career,conj_5B_career),
factor_marstat=c(conj_1A_marstat,conj_1B_marstat,conj_2A_marstat,conj_2B_marstat,conj_3A_marstat,conj_3B_marstat,conj_4A_marstat,conj_4B_marstat,conj_5A_marstat,conj_5B_marstat),
factor_sexorient=c(conj_1A_sexorient,conj_1B_sexorient,conj_2A_sexorient,conj_2B_sexorient,conj_3A_sexorient,conj_3B_sexorient,conj_4A_sexorient,conj_4B_sexorient,conj_5A_sexorient,conj_5B_sexorient),
##DV
conj_choice=c(ifelse(conj_1==1,1,0),ifelse(conj_1==2,1,0),ifelse(conj_2==1,1,0),ifelse(conj_2==2,1,0),ifelse(conj_3==1,1,0),ifelse(conj_3==2,1,0),ifelse(conj_4==1,1,0),ifelse(conj_4==2,1,0),ifelse(conj_5==1,1,0),ifelse(conj_5==2,1,0))
)

detach(data)

stacked$factor_agebin <- ifelse(stacked$factor_age < 35 & stacked$factor_age >=25,1,
                                ifelse(stacked$factor_age < 45 & stacked$factor_age >= 35,2,
                                       ifelse(stacked$factor_age < 55 & stacked$factor_age >= 45,3,
                                              ifelse(stacked$factor_age < 65 & stacked$factor_age >= 55,4,
                                                     ifelse(stacked$factor_age < 76 & stacked$factor_age >= 65,5,
                                                            6)))))

####Now to convert to factor variables

 stacked$var_race <- factor(stacked$factor_race,levels=c(1,2,3,4,5),labels=c("Black","White","Hispanic/Latino","Asian","Race - Not Asked"))
 stacked$var_sex <- factor(stacked$factor_sex,levels=c(1,2,3),labels=c("Male","Female","Sex - Not Asked"))
 stacked$var_age <- factor(stacked$factor_agebin,levels=c(1,2,3,4,5,6),labels=c("25-34","35-44","45-54","55-64","65-75","Age - Not Asked"))
 stacked$var_party <- factor(stacked$factor_party,levels=c(1,2,3,4),labels=c("Democrat","Republican","Independent","Party - Not Asked"))
 stacked$var_ideo <- factor(stacked$factor_ideo,levels=c(1,2,3,4,5),labels=c("Fiscally Conservative","Socially Conservative","Fiscally Liberal","Socially Liberal","Ideology - Not Asked"))
 stacked$var_grew <- factor(stacked$factor_grew,levels=c(1,2,3,4,5),labels=c("Big City","Small Town","Suburbs","Medium Size City","Grew Up - Not Asked"))
 stacked$var_relig <- factor(stacked$factor_relig,levels=c(1,2,3,4,5,6,7,8),labels=c("Atheist","Jewish","Muslim","Catholic","Mainline Protestant","Evangelical Protestant","Spiritual","Religion - Not Asked"))
 stacked$var_career <- factor(stacked$factor_career,levels=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16),labels=c("Carpenter","Professor","Teacher","Lawyer","Doctor","CEO","Retail Manager","Receptionist","Small-Business Owner","Farmer","Factor Foreman","Construction Worker","Engineer","Lobbyist","Political Staffer","Career - Not Asked"))
 stacked$var_marstat <- factor(stacked$factor_marstat,levels=c(1,2,3,4,5),labels=c("Married","Single","Divorced","Widowed","Marital Status - Not Asked"))
 stacked$var_sexorient <- factor(stacked$factor_sexorient,levels=c(1,2,4),labels=c("Homosexual, Gay, or LGBT","Heterosexual or Straight","Sexual Orientation - Not Asked"))



####For breaking out by party
stacked$resp_pid <- recode(stacked$resp_pid7,"1=-1;2=-1;3=-1;4=0;5=1;6=1;7=1;else=NA")

stacked$conj_choice_factor <- factor(stacked$conj_choice,levels=c(0,1),labels=c("Not Chosen","Chosen"))

formula = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_party+var_sexorient+var_marstat+var_age+var_sex+var_race

stacked$resp_newsint <- ifelse(stacked$newsint==1,1,0)

###########################
###For the types of conjoint tasks:
t_relative <- subset(stacked,stacked$conj_treat==1)
t_neighbor <- subset(stacked,stacked$conj_treat==2)
t_loan <- subset(stacked,stacked$conj_treat==3)
t_charity <- subset(stacked,stacked$conj_treat==4)
t_jury <- subset(stacked,stacked$conj_treat==5)

####Getting the marginal means for subgroups

relative <- mm(t_relative, formula, id = ~caseid)
relative$task <- "Relative"

neighbor <- mm(t_neighbor, formula, id = ~caseid)
neighbor$task <- "Neighbor"

loan <-  mm(t_loan, formula, id = ~caseid)
loan$task <- "Loan"

charity <-  mm(t_charity, formula, id = ~caseid)
charity$task <- "Charity"

jury <-  mm(t_jury, formula, id = ~caseid)
jury$task <- "Jury"

#Bringing it together
overall <- rbind(relative[,c(3:5,9:11)],neighbor[,c(3:5,9:11)],loan[,c(3:5,9:11)],charity[,c(3:5,9:11)],jury[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_bytask.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=task, shape=task)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(15,16,17,18,3),guide=guide_legend(reverse=T))+
    scale_color_manual(values = c("black","gray20","gray40","gray60","gray80"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.3,0.7) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=38.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=41.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=46.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=52.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=55.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

###########################
####Now for by party

dems <- subset(stacked,stacked$resp_pid==-1)
inds <- subset(stacked,stacked$resp_pid==0)
reps <- subset(stacked,stacked$resp_pid==1)


dem <- mm(dems, formula, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula, id = ~caseid)
rep$pid <- "Republican"

overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byparty.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.3,0.7) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=38.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=41.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=46.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=52.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=55.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

#####OVERALL

stacked_rep <- subset(stacked,stacked$weight!="")
overall <- mm(stacked_rep, formula, id = ~caseid, weights= ~weight)

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_overall.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, group=1)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
 #scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  #scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.3,0.7) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=38.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=41.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=46.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=52.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=55.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

####BY RACE AFFINITIES

r_white <- subset(stacked,stacked$resp_race==1)
r_black <- subset(stacked,stacked$resp_race==2)
r_hisp <- subset(stacked,stacked$resp_race==3)
r_asian <- subset(stacked,stacked$resp_race==4)

white <- mm(r_white, formula, id = ~caseid)
white$race <- "White"

black <- mm(r_black, formula, id = ~caseid)
black$race <- "Black"

hisp <- mm(r_hisp, formula, id = ~caseid)
hisp$race <- "Hispanic/Latino"

asian <- mm(r_asian, formula, id = ~caseid)
asian$race <- "Asian"

overall <- rbind(white[,c(3:5,9:11)],black[,c(3:5,9:11)],hisp[,c(3:5,9:11)],asian[,c(3:5,9:11)])

overall <- subset(overall,overall$feature=="var_race")

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byrace.pdf", width=9, height=5, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=race, shape=race)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(15,16,17,18,3),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("black","gray20","gray40","gray60"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.3,0.7) + 
  theme_bw() +
  #theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")
dev.off()
detach(overall)

####BY SEX & SEXUAL ORIENTATION AFFINITIES

s_m_hetero <- subset(stacked,stacked$resp_sex==1 & stacked$resp_sexorient==1)
s_w_hetero <- subset(stacked,stacked$resp_sex==2 & stacked$resp_sexorient==1)
s_m_lgbt <- subset(stacked,stacked$resp_sex==1 & stacked$resp_sexorient>1 & stacked$resp_sexorient<4)
s_w_lgbt <- subset(stacked,stacked$resp_sex==2 & stacked$resp_sexorient>1 & stacked$resp_sexorient<4)

m_hetero <- mm(s_m_hetero, formula, id = ~caseid)
m_hetero$sex <- "Male - Heterosexual"

w_hetero <- mm(s_w_hetero, formula, id = ~caseid)
w_hetero$sex <- "Female - Heterosexual"

m_lgbt <- mm(s_m_lgbt, formula, id = ~caseid)
m_lgbt$sex <- "Male - LGBT"

w_lgbt <- mm(s_w_lgbt, formula, id = ~caseid)
w_lgbt$sex <- "Female - LGBT"

overall <- rbind(m_hetero[,c(3:5,9:11)],w_hetero[,c(3:5,9:11)],m_lgbt[,c(3:5,9:11)],w_lgbt[,c(3:5,9:11)])

overall <- subset(overall,overall$feature=="var_sex" | overall$feature=="var_sexorient")

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_bysex.pdf", width=9, height=5, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=sex, shape=sex)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(15,16,17,18,3),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("black","gray20","gray40","gray60"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.3,0.7) + 
  theme_bw() +
  #theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.85,0.30),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")
dev.off()
detach(overall)


#####Now for religious affinities

rel_atheist <- subset(stacked,stacked$resp_relig==9)
rel_jewish <- subset(stacked,stacked$resp_relig==5)
rel_muslim <- subset(stacked,stacked$resp_relig==6)
rel_catholic <- subset(stacked,stacked$resp_relig==2)
rel_mainline <- subset(stacked,stacked$resp_relig==1 & stacked$resp_evang==2)
rel_evang <- subset(stacked,stacked$resp_relig==1 & stacked$resp_evang==1)
rel_spiritual <- subset(stacked,stacked$resp_relig>10)

atheist <- mm(rel_atheist, formula, id = ~caseid)
atheist$rel <- "Atheist"
jewish <- mm(rel_jewish, formula, id = ~caseid)
jewish$rel <- "Jewish"
muslim <- mm(rel_muslim, formula, id = ~caseid)
muslim$rel <- "Muslim"
catholic <- mm(rel_catholic, formula, id = ~caseid)
catholic$rel <- "Catholic"
mainline <- mm(rel_mainline, formula, id = ~caseid)
mainline$rel <- "Mainline Protestant"
evang <- mm(rel_evang, formula, id = ~caseid)
evang$rel <- "Evangelical Protestant"
spiritual <- mm(rel_spiritual, formula, id = ~caseid)
spiritual$rel <- "Spiritual"

overall <- rbind(atheist[,c(3:5,9:11)],jewish[,c(3:5,9:11)],muslim[,c(3:5,9:11)],catholic[,c(3:5,9:11)],mainline[,c(3:5,9:11)],evang[,c(3:5,9:11)],spiritual[,c(3:5,9:11)])

overall <- subset(overall,overall$feature=="var_relig")


###Plotting
attach(overall)
pdf("mm_byrelig.pdf", width=9, height=5, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=rel, shape=rel)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(15,16,17,18,3,4,5),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("black","gray15","gray30","gray45","gray60","gray75","gray90"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.2,0.8) + 
  theme_bw() +
  #theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.87,0.65),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")
dev.off()
detach(overall)

#########
#########
#########
#########NOW FOR INTERACTIONS

party_int <- CausalANOVA(formula=formula,int2.formula= ~ var_party:var_ideo,data=stacked,cluster=stacked$caseid,nway=2)

plot(party_int,fac.name=c("var_party","var_ideo"),type="AMIE")

race_int <- CausalANOVA(formula=formula,int2.formula= ~ var_party:var_race,data=stacked,cluster=stacked$caseid,nway=2)

#########NOW FOR INTERACTIONS by task

stacked$task <- factor(stacked$conj_treat,levels=c(1,2,3,4,5),labels=c("Relative","Neighbor","Loan","Charity","Jury"))

formula2 = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_party+var_sexorient+var_marstat+var_age+var_sex+var_race+task

task_int <- CausalANOVA(formula=formula2,int2.formula= ~ task:var_career+task:var_grew+task:var_relig+task:var_ideo+task:var_party+task:var_sexorient+task:var_marstat+task:var_age+task:var_sex+task:var_race,data=stacked,cluster=stacked$caseid,nway=2)

##Now pull out all the interaction effects

temp <- task_int$CI.table

write.csv(temp[[12]],"AMIE_career.csv")
write.csv(temp[[13]],"AMIE_grew.csv")
write.csv(temp[[14]],"AMIE_relig.csv")
write.csv(temp[[15]],"AMIE_ideo.csv")
write.csv(temp[[16]],"AMIE_party.csv")
write.csv(temp[[17]],"AMIE_sexorient.csv")
write.csv(temp[[18]],"AMIE_marstat.csv")
write.csv(temp[[19]],"AMIE_age.csv")
write.csv(temp[[20]],"AMIE_sex.csv")
write.csv(temp[[21]],"AMIE_race.csv")

#####

#1 = Dem, 2 = Rep, 3 = Ind, 4 = Not Asked
#1 = Fisc Cons, 2 = Soc Cons, 3 = Fisc Lib, 4 = Fisc Cons, 5 = Not Asked

stacked$party_ideo <- ifelse(stacked$factor_party==1 & stacked$factor_ideo==1,1,
                             ifelse(stacked$factor_party==1 & stacked$factor_ideo==2,2,
                                    ifelse(stacked$factor_party==1 & stacked$factor_ideo==3,3,
                                           ifelse(stacked$factor_party==1 & stacked$factor_ideo==4,4,
                                                  ifelse(stacked$factor_party==1 & stacked$factor_ideo==5,5,
                                                         ifelse(stacked$factor_party==2 & stacked$factor_ideo==1,6,
                                                                ifelse(stacked$factor_party==2 & stacked$factor_ideo==2,7,
                                                                       ifelse(stacked$factor_party==2 & stacked$factor_ideo==3,8,
                                                                              ifelse(stacked$factor_party==2 & stacked$factor_ideo==4,9,
                                                                                     ifelse(stacked$factor_party==2 & stacked$factor_ideo==5,10,
                                                                                            ifelse(stacked$factor_party==3 & stacked$factor_ideo==1,11,
                                                                                                   ifelse(stacked$factor_party==3 & stacked$factor_ideo==2,12,
                                                                                                          ifelse(stacked$factor_party==3 & stacked$factor_ideo==3,13,
                                                                                                                 ifelse(stacked$factor_party==3 & stacked$factor_ideo==4,14,
                                                                                                                        ifelse(stacked$factor_party==3 & stacked$factor_ideo==5,15,
                                                                                                                               ifelse(stacked$factor_party==4 & stacked$factor_ideo==1,16,
                                                                                                                                      ifelse(stacked$factor_party==4 & stacked$factor_ideo==2,17,
                                                                                                                                             ifelse(stacked$factor_party==4 & stacked$factor_ideo==3,18,
                                                                                                                                                    ifelse(stacked$factor_party==4 & stacked$factor_ideo==4,19,
                                                                                                                                                           ifelse(stacked$factor_party==4 & stacked$factor_ideo==5,20,NA))))))))))))))))))))


stacked$party_ideo_f <- factor(stacked$party_ideo,levels=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),labels=c("Democrat / Fiscally Conservative","Democrat / Socially Conservative","Democrat / Fiscally Liberal","Democrat / Socially Liberal","Democrat / Not Asked","Republican / Fiscally Conservative","Republican / Socially Conservative","Republican / Fiscally Liberal","Republican / Socially Liberal","Republican / Not Asked","Independent / Fiscally Conservative","Independent / Socially Conservative","Independent / Fiscally Liberal","Independent / Socially Liberal","Independent / Not Asked","Not Asked / Fiscally Conservative","Not Asked / Socially Conservative","Not Asked / Fiscally Liberal","Not Asked / Socially Liberal","Not Asked / Not Asked"))


formula_alt = conj_choice ~ var_career+var_grew+var_relig+var_sexorient+var_marstat+var_age+var_sex+var_race+party_ideo_f


dems <- subset(stacked,stacked$resp_pid==-1)
inds <- subset(stacked,stacked$resp_pid==0)
reps <- subset(stacked,stacked$resp_pid==1)


dem <- mm(dems, formula_alt, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula_alt, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula_alt, id = ~caseid)
rep$pid <- "Republican"


overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

overall <- subset(overall,overall$feature=="party_ideo_f")

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_partyideo.pdf", width=9, height=7, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.2,0.8) + 
  theme_bw() +
  #theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.87,0.95),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=5.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=10.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=15.5,size=0.5,linetype="longdash") 
dev.off()
detach(overall)

############NOW TO REPEAT WITH RACE INTERACTION

#####

#### stacked$var_race <- factor(stacked$factor_race,levels=c(1,2,3,4,5),labels=c("Black","White","Hispanic/Latino","Asian","Race - Not Asked"))

#1 = Dem, 2 = Rep, 3 = Ind, 4 = Not Asked
#1 = Fisc Cons, 2 = Soc Cons, 3 = Fisc Lib, 4 = Fisc Cons, 5 = Not Asked

stacked$party_race <- ifelse(stacked$factor_party==1 & stacked$factor_race==1,1,
                             ifelse(stacked$factor_party==1 & stacked$factor_race==2,2,
                                    ifelse(stacked$factor_party==1 & stacked$factor_race==3,3,
                                           ifelse(stacked$factor_party==1 & stacked$factor_race==4,4,
                                                  ifelse(stacked$factor_party==1 & stacked$factor_race==5,5,
                                                         ifelse(stacked$factor_party==2 & stacked$factor_race==1,6,
                                                                ifelse(stacked$factor_party==2 & stacked$factor_race==2,7,
                                                                       ifelse(stacked$factor_party==2 & stacked$factor_race==3,8,
                                                                              ifelse(stacked$factor_party==2 & stacked$factor_race==4,9,
                                                                                     ifelse(stacked$factor_party==2 & stacked$factor_race==5,10,
                                                                                            ifelse(stacked$factor_party==3 & stacked$factor_race==1,11,
                                                                                                   ifelse(stacked$factor_party==3 & stacked$factor_race==2,12,
                                                                                                          ifelse(stacked$factor_party==3 & stacked$factor_race==3,13,
                                                                                                                 ifelse(stacked$factor_party==3 & stacked$factor_race==4,14,
                                                                                                                        ifelse(stacked$factor_party==3 & stacked$factor_race==5,15,
                                                                                                                               ifelse(stacked$factor_party==4 & stacked$factor_race==1,16,
                                                                                                                                      ifelse(stacked$factor_party==4 & stacked$factor_race==2,17,
                                                                                                                                             ifelse(stacked$factor_party==4 & stacked$factor_race==3,18,
                                                                                                                                                    ifelse(stacked$factor_party==4 & stacked$factor_race==4,19,
                                                                                                                                                           ifelse(stacked$factor_party==4 & stacked$factor_race==5,20,NA))))))))))))))))))))


stacked$party_race_f <- factor(stacked$party_race,levels=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),labels=c("Democrat / Black","Democrat / White","Democrat / Hispanic-Latino","Democrat / Asian","Democrat / Not Asked","Republican / Black","Republican / White","Republican / Hispanic-Latino","Republican / Asian","Republican / Not Asked","Independent / Black","Independent / White","Independent / Hispanic-Latino","Independent / Asian","Independent / Not Asked","Not Asked / Black","Not Asked / White","Not Asked / Hispanic-Latino","Not Asked / Asian","Not Asked / Not Asked"))


formula_alt = conj_choice ~ var_career+var_grew+var_relig+var_sexorient+var_marstat+var_age+var_sex+var_ideo+party_race_f


dems <- subset(stacked,stacked$resp_pid==-1)
inds <- subset(stacked,stacked$resp_pid==0)
reps <- subset(stacked,stacked$resp_pid==1)


dem <- mm(dems, formula_alt, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula_alt, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula_alt, id = ~caseid)
rep$pid <- "Republican"


overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

overall <- subset(overall,overall$feature=="party_race_f")

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_partyrace.pdf", width=9, height=7, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.2,0.8) + 
  theme_bw() +
  #theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.87,0.95),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=5.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=10.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=15.5,size=0.5,linetype="longdash") 
dev.off()
detach(overall)


#################################################
######NOW FOR HIGH/LOW KNOWLEDGE

###########################
####Now for by party w/ knowledge

dems_high <- subset(stacked,stacked$resp_pid==-1 & stacked$resp_newsint==1)
dems_low <- subset(stacked,stacked$resp_pid==-1 & stacked$resp_newsint==0)
inds_high <- subset(stacked,stacked$resp_pid==0 & stacked$resp_newsint==1)
inds_low <- subset(stacked,stacked$resp_pid==0 & stacked$resp_newsint==0)
reps_high <- subset(stacked,stacked$resp_pid==1 & stacked$resp_newsint==1)
reps_low <- subset(stacked,stacked$resp_pid==1 & stacked$resp_newsint==0)

dem_high <- mm(dems_high, formula, id = ~caseid)
dem_high$pid <- "Democrat + High Interest"

dem_low <- mm(dems_low, formula, id = ~caseid)
dem_low$pid <- "Democrat + Low Interest"

ind_high <- mm(inds_high, formula, id = ~caseid)
ind_high$pid <- "Independent + High Interest"

ind_low <- mm(inds_low, formula, id = ~caseid)
ind_low$pid <- "Independent + Low Interest"

rep_high <- mm(reps_high, formula, id = ~caseid)
rep_high$pid <- "Republican + High Interest"

rep_low <- mm(reps_low, formula, id = ~caseid)
rep_low$pid <- "Republican + Low Interest"

overall <- rbind(dem_high[,c(3:5,9:11)],dem_low[,c(3:5,9:11)],ind_high[,c(3:5,9:11)],ind_low[,c(3:5,9:11)],rep_high[,c(3:5,9:11)],rep_low[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_partyinterest.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,1,4,5,17,2),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","lightskyblue","gray20","gray50","red","indianred"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.25,0.75) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.86,0.41),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=38.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=41.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=46.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=52.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=55.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

###Plotting w/o independent
overall <- subset(overall,overall$pid=="Democrat + High Interest"|overall$pid=="Democrat + Low Interest"|overall$pid=="Republican + High Interest"|overall$pid=="Republican + Low Interest")

attach(overall)
pdf("mm_partyinterest_noind.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,1,17,2),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","lightskyblue","red","indianred"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.25,0.75) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.86,0.41),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=38.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=41.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=46.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=52.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=55.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

##################################################################################
##################################################################################

####Now for by party by ``not asked'' and specific experimentally manipulated partisanship. So 4 different plots, one for each party condition

##Not asked first

notasked <- subset(stacked,stacked$var_party=="Party - Not Asked")

dems <- subset(notasked,notasked$resp_pid==-1)
inds <- subset(notasked,notasked$resp_pid==0)
reps <- subset(notasked,notasked$resp_pid==1)

#Fix the formula
formula = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_sexorient+var_marstat+var_age+var_sex+var_race


dem <- mm(dems, formula, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula, id = ~caseid)
rep$pid <- "Republican"

overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byparty_notasked.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.15,0.85) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=37.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=42.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=48.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=51.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=56.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

#########Republicans only


notasked <- subset(stacked,stacked$var_party=="Republican")

dems <- subset(notasked,notasked$resp_pid==-1)
inds <- subset(notasked,notasked$resp_pid==0)
reps <- subset(notasked,notasked$resp_pid==1)

#Fix the formula
formula = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_sexorient+var_marstat+var_age+var_sex+var_race


dem <- mm(dems, formula, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula, id = ~caseid)
rep$pid <- "Republican"

overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byparty_republican.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.15,0.85) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=37.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=42.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=48.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=51.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=56.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)


#########Democrats only


notasked <- subset(stacked,stacked$var_party=="Democrat")

dems <- subset(notasked,notasked$resp_pid==-1)
inds <- subset(notasked,notasked$resp_pid==0)
reps <- subset(notasked,notasked$resp_pid==1)

#Fix the formula
formula = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_sexorient+var_marstat+var_age+var_sex+var_race


dem <- mm(dems, formula, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula, id = ~caseid)
rep$pid <- "Republican"

overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byparty_democrat.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.15,0.85) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=37.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=42.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=48.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=51.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=56.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)

#########Independents only


notasked <- subset(stacked,stacked$var_party=="Independent")

dems <- subset(notasked,notasked$resp_pid==-1)
inds <- subset(notasked,notasked$resp_pid==0)
reps <- subset(notasked,notasked$resp_pid==1)

#Fix the formula
formula = conj_choice ~ var_career+var_grew+var_relig+var_ideo+var_sexorient+var_marstat+var_age+var_sex+var_race


dem <- mm(dems, formula, id = ~caseid)
dem$pid <- "Democrat"

ind <- mm(inds, formula, id = ~caseid)
ind$pid <- "Independent"

rep <- mm(reps, formula, id = ~caseid)
rep$pid <- "Republican"

overall <- rbind(dem[,c(3:5,9:11)],ind[,c(3:5,9:11)],rep[,c(3:5,9:11)])

pd <- position_dodge(.8)

###Plotting
attach(overall)
pdf("mm_byparty_independent.pdf", width=11, height=13, pointsize=10)
ggplot(overall, 
       aes(x=level, y=estimate, color=pid, shape=pid)) + 
  geom_hline(yintercept=0.5,size=0.5,color="gray70") +
  scale_shape_manual(values=c(16,4,17),guide=guide_legend(reverse=T))+
  scale_color_manual(values = c("blue","gray50","red"),guide=guide_legend(reverse=T)) +
  #scale_x_discrete(limits=rev(X)) + 
  geom_errorbar(width=.1, aes(ymin=lower, ymax=upper),position=pd) +
  geom_point(size=2,position=pd) +
  ylim(0.15,0.85) + 
  theme_bw() +
  theme(axis.text.y = element_text(hjust=0,color="gray40",face=c("plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold","plain","plain","plain","plain","plain","bold","plain","plain","bold","plain","plain","plain","plain","bold"))) +
  theme(legend.position=c(0.91,0.15),legend.title=element_blank(),legend.key=element_rect(fill="transparent"),legend.background=element_rect(fill="transparent")) +
  coord_flip() + 
  labs(x = "Experimentally Manipulated Variable", y = "Estimated Choice Probability")+
  geom_vline(xintercept=16.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=21.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=29.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=34.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=37.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=42.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=48.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=51.5,size=0.5,linetype="longdash")+
  geom_vline(xintercept=56.5,size=0.5,linetype="longdash")
dev.off()
detach(overall)


###END
###############################################################
###############################################################